package com.vaadin.pekka.treegridwidget.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TreeGridElementBuilder {

    public static final String LOWEST_COLUMN_HEADER = VTreeGrid.CLASSNAME
            + "-lowestheader";
    public static final String HIGHEST_COLUMN_HEADER = VTreeGrid.CLASSNAME
            + "-highestheader";
    public static final String COLUMN_HEADER = VTreeGrid.CLASSNAME
            + "-columnheader";
    public static final String COLUMN_HEADER_NAME = VTreeGrid.CLASSNAME
            + "-headername";
    public static final String ROW_HEADER = VTreeGrid.CLASSNAME + "-rowheader";
    public static final String ROW_HEADER_NAME = VTreeGrid.CLASSNAME
            + "-headername";
    public static final String COLUMN_HEADER_CELL = VTreeGrid.CLASSNAME
            + "-columnheadercell";
    public static final String ROW_HEADER_CELL = VTreeGrid.CLASSNAME
            + "-rowheadercell";
    public static final String LOWEST_HEADER_CELL = VTreeGrid.CLASSNAME
            + "-lowestheadercell";

    public Label getHighestHeaderLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(HIGHEST_COLUMN_HEADER);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Element getHighestHeaderCell(String value) {
        Element e = Document.get().createDivElement();
        e.setClassName(HIGHEST_COLUMN_HEADER);
        e.setInnerText(value);
        return e;
    }

    // public String getHighestHeaderCell(String value) {
    // return "<div style=\"width: 100%; height: 100%;"
    // + " text-align: center; vertical-align: middle;"
    // + " font-weight: bold;" + " background: blue;"
    // + " color: white;" + " \">" + value + "</div>";
    // }

    public Label getRowHeaderNameLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(COLUMN_HEADER_NAME);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Label getHeaderNameLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(COLUMN_HEADER_NAME);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Element getHeaderNameCell(String value) {
        Element e = Document.get().createDivElement();
        e.setClassName(COLUMN_HEADER_NAME);
        e.setInnerText(value);
        return e;
    }

    // public String getHeaderNameCell(String value) {
    // return "<div style=\"width: 100%; height: 100%;"
    // + " text-align: center; vertical-align: middle;"
    // + " font-weight: bold;" + " background: darkblue;"
    // + " color: white;" + " \">" + value + "</div>";
    // }

    public Label getHeaderLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(COLUMN_HEADER);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Element getHeaderCell(String value) {
        Element e = Document.get().createDivElement();
        e.setClassName(COLUMN_HEADER);
        e.setInnerText(value);
        return e;
    }

    // public String getHeaderCell(String value) {
    // return "<div style=\"width: 100%; height: 100%;"
    // + " display: table-cell;"
    // + " text-align: center; vertical-align: middle;"
    // + " font-weight: bold;" + " background: dodgerblue;"
    // + " color: white;" + " \">" + value + "</div>";
    // }

    public Label getLowestHeaderLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(LOWEST_COLUMN_HEADER);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Element getLowestHeaderCell(String value) {
        Element e = Document.get().createDivElement();
        e.setClassName(LOWEST_COLUMN_HEADER);
        e.setInnerText(value);
        return e;
    }

    // public String getLowestHeaderCell(String value) {
    // return "<div style=\"width: 100%; height: 100%;"
    // + " display: table-cell;"
    // + " text-align: center; vertical-align: middle;"
    // + " font-weight: bold;" + " background: grey;"
    // + " color: white;" + " \">" + value + "</div>";
    // }

    public Label getRowHeaderLabel(String value) {
        Label l = new Label(value);
        l.setStylePrimaryName(ROW_HEADER);
        l.setWidth("100%");
        l.setHeight("100%");
        return l;
    }

    public Element getRowHeaderCell(String value) {
        Element e = Document.get().createDivElement();
        e.setClassName(ROW_HEADER);
        e.setInnerText(value);
        return e;
    }

    // public String getRowHeaderCell(String value) {
    // return "<div style=\"width: 100%; height: 100%; "
    // + " display: table-row;"
    // + " text-align: center; vertical-align: middle;"
    // + " font-weight: bold;" + " background: blue;"
    // + " color: white;" + " \">" + value + "</div>";
    // }

    public HorizontalPanel buildColumnHeaderCell(String[] values, int repeat) {
        HorizontalPanel p = new HorizontalPanel();
        p.setSpacing(1);
        p.setWidth("100%");
        p.setHeight("100%");
        p.setStylePrimaryName(COLUMN_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                p.add(getHeaderLabel(values[i]));
            }
        }
        return p;
    }

    public void buildHeaderCell(String[] values, int repeat, Element cell) {
        cell.setClassName(COLUMN_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                cell.appendChild(getHeaderCell(values[i]));
            }
        }
    }

    // public String getHeaderCell(String[] values, int repeat) {
    // StringBuilder sb = new StringBuilder();
    // for (int r = 0; r < repeat; r++) {
    // for (int i = 1; i < values.length; i++) {
    // sb.append(getHeaderCell(values[i]));
    // }
    // }
    // return sb.toString();
    // }

    public HorizontalPanel buildLowestHeaderCell(String[] values, int repeat) {
        HorizontalPanel p = new HorizontalPanel();
        p.setSpacing(1);
        p.setWidth("100%");
        p.setHeight("100%");
        p.setStylePrimaryName(LOWEST_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                p.add(getLowestHeaderLabel(values[i]));
            }
        }
        return p;
    }

    public void buildLowestHeaderCell(String[] values, int repeat,
            final Element cell) {
        cell.setClassName(LOWEST_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                cell.appendChild(getLowestHeaderCell(values[i]));
            }
        }
    }

    // public String getLowestHeaderCell(String[] values, int repeat) {
    // StringBuilder sb = new StringBuilder();
    // for (int r = 0; r < repeat; r++) {
    // for (int i = 1; i < values.length; i++) {
    // sb.append(getLowestHeaderCell(values[i]));
    // }
    // }
    // return sb.toString();
    // }

    public VerticalPanel buildRowHeaderCell(String[] values, int repeat) {
        VerticalPanel p = new VerticalPanel();
        p.setSpacing(1);
        p.setWidth("100%");
        p.setHeight("100%");
        p.setStylePrimaryName(ROW_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                p.add(getRowHeaderLabel(values[i]));
            }
        }
        return p;
    }

    public void buildRowHeaderCell(String[] values, int repeat, Element cell) {
        cell.setClassName(ROW_HEADER_CELL);
        for (int r = 0; r < repeat; r++) {
            for (int i = 1; i < values.length; i++) {
                cell.appendChild(getRowHeaderCell(values[i]));
            }
        }
    }
    // public String getRowHeaderCell(String[] values, int repeat) {
    // StringBuilder sb = new StringBuilder();
    // for (int r = 0; r < repeat; r++) {
    // for (int i = 1; i < values.length; i++) {
    // sb.append(getRowHeaderCell(values[i]));
    // }
    // }
    // return sb.toString();
    // }

}
